layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
        url : 'getUsers',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'punsterName', title: '账号', align:"center"},
            {field: 'nickName', title: '昵称', align:'center',edit:"text"},
            {field: 'pwd', title: '密码', align:'center',edit:"text",templet:function (d) {
                   return "********";
                }},
            {field: 'state', title: '账号状态', align:'center',templet:"#Status"},
            {field: 'sex', title: '性别',  align:'center',templet:function (d) {
                    if (null == d.sex) {
                        return "无";
                    } else {
                        return d.sex;
                    }
                }},
            {field: 'picture', title: '头像', align:'center',edit:"text"},
            {field: 'fansNum', title: '粉丝', align:'center',edit:"text"},
            {field: 'focusNum', title: '关注', align:'center',edit:"text"},
            {field: 'birthday', title: '生日', align:'center',templet:function (d) {
                    if (null == d.birthday) {
                        return "无";
                    } else {
                        return d.birthday;
                    }
                }},
            {field: 'email', title: '邮箱', align:'center',templet:function (d) {
                    if (null == d.email) {
                        return "无";
                    } else {
                        return d.email;
                    }
                }},
            {field: 'myEvaluation', title: '介绍', align:'center',edit:"text",templet:function (d) {
                    if (null == d.myEvaluation) {
                        return "无";
                    } else {
                        return d.myEvaluation;
                    }
                }},
            {title: '操作', minWidth:125, templet:'#userListBar',fixed:"right",align:"center"}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("userListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });
    /**
     * 刷新表格
     */
    $(".refresh_btn").click(function(){
        tableIns.reload();
    })


    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data,
            userId = "";
        if(data.length > 0) {
            for (var i in data) {
                userId+= ","+ data[i].userId;
            }
            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                $.get("deleteAllUser",{
                    userId : userId  //将需要删除的articlesId作为参数传入
                },function(result){
                    if (result.code == 100) {
                        tableIns.reload();
                        layer.close(index);
                        setTimeout(function(){
                            layer.msg("删除成功");
                        },500);
                    } else {
                        layer.msg("删除失败");
                    }
                })
            })
        }else{
            layer.msg("请选择需要删除的文章");
        }
    })
    //表格监听编辑
    table.on('edit(userList)', function(obj){
        $.ajax({
            url : "updateUser",
            type:"get",
            data : {
                field:obj.field,
                value:obj.value,
                userId:obj.data.userId
            },
            success : function (result) {
                if (result.code == 100){
                    layer.msg("更新成功")
                } else {
                    layer.msg("更新失败")
                }
            }
        });
    });


    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'usable'){ //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if(_this.text()=="已禁用"){
                usableText = "是否确定启用此用户？",
                btnText = "已启用";
            }
            layer.confirm(usableText,{
                icon: 3,
                title:'系统提示',
                cancel : function(index){
                    layer.close(index);
                }
            },function(index){  //确定
                $.ajax({
                    url : "updateUserState",
                    type : "GET",
                    data :{
                        userId : data.userId,
                        state : data.state
                    },
                    success : function (result) {
                        if (result.code == 100 ){
                            tableIns.reload();
                            layer.close(index);
                            setTimeout(function(){
                                layer.msg("操作成功");
                            },500);
                        } else {
                            layer.close(index);
                            setTimeout(function(){
                                layer.msg("操作失败");
                            },500);
                        }
                    }
                })
            },function(index){
                layer.close(index);
            });
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
                $.get("deleteUserByUserId",{
                    userId : data.userId  //将需要删除的Id作为参数传入
                },function(result){
                    if (result.code == 100 ) {
                        tableIns.reload();
                        layer.close(index);
                        setTimeout(function(){
                            layer.msg("删除成功");
                        },500);
                    } else {
                        setTimeout(function(){
                            layer.msg("删除失败");
                        },500);
                    }
                })
            });
        }
    });
})
